/**
 * Copyright &copy; 2013-2016 山东易科德软件有限公司   All rights reserved.
 */
package cn.net.ecode.elec.plan.web;

import cn.net.ecode.common.config.Global;
import cn.net.ecode.common.persistence.Page;
import cn.net.ecode.common.utils.DateUtils;
import cn.net.ecode.common.utils.excel.ExportExcel;
import cn.net.ecode.common.web.BaseController;
import cn.net.ecode.elec.plan.entity.ElecPlan;
import cn.net.ecode.elec.plan.service.ElecPlanService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 报电量Controller
 *
 * @author AlanShelby
 * @version 2019-08-28
 */
@Controller
@RequestMapping(value = "${adminPath}/plan/elecPlan")
public class ElecPlanController extends BaseController {

    @Autowired
    private ElecPlanService elecPlanService;

    /**
     * 获取数据
     */
    @ModelAttribute
    public ElecPlan get(String id, boolean isNewRecord) {
        return elecPlanService.get(id, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("plan:elecPlan:view")
    @RequestMapping(value = {"list", ""})
    public String list(ElecPlan elecPlan, Model model) {
        model.addAttribute("elecPlan", elecPlan);
        return "elec/plan/elecPlanList";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("plan:elecPlan:view")
    @ResponseBody
    @RequestMapping(value = "listData")
    public Page<ElecPlan> listData(ElecPlan elecPlan, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ElecPlan> page = elecPlanService.findPage(new Page<ElecPlan>(request, response), elecPlan);
        return page;
    }

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("plan:elecPlan:view")
    @RequestMapping(value = "form")
    public String form(ElecPlan elecPlan, Model model) {
        model.addAttribute("elecPlan", elecPlan);
        return "elec/plan/elecPlanForm";
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("plan:elecPlan:edit")
    @ResponseBody
    @RequestMapping(value = "save")
    public String save(ElecPlan elecPlan) {
        StringBuilder message = new StringBuilder();
        if (!beanValidator(message, elecPlan)) {
            return renderResult(Global.FALSE, message);
        }
        elecPlanService.save(elecPlan);
        return renderResult(Global.TRUE, "保存报电量成功！");
    }

    /**
     * 删除数据
     */
    @RequiresPermissions("plan:elecPlan:edit")
    @ResponseBody
    @RequestMapping(value = "delete")
    public String delete(ElecPlan elecPlan) {
        elecPlanService.delete(elecPlan);
        return renderResult(Global.TRUE, "删除报电量成功！");
    }

    @RequestMapping(value = "export", method = RequestMethod.POST)
    public String exportFile(ElecPlan elecPlan, HttpServletRequest request,
                             HttpServletResponse response, RedirectAttributes redirectAttributes) {
        try {
            String fileName = "报电量" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
            List<ElecPlan> list = elecPlanService.findList(elecPlan);
            new ExportExcel("报电量", ElecPlan.class).setDataList(list).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            addMessage(redirectAttributes, "导出报电量失败！失败信息：" + e.getMessage());
        }
        return "redirect:" + adminPath + "/plan/elecPlan/?repage";
    }
}